﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;

namespace leaf.Base
{
    /// <summary>
    /// Http请求工具类
    /// </summary>
    public static class HttpSay
    {
        public static T Get<T>(string uri)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            httpWebRequest.Method = "GET";
            httpWebRequest.Timeout = 20000;
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
            string rspContent = streamReader.ReadToEnd();
            Global.log.Info(uri);
            Global.log.Info("[GET] " + rspContent);
            Global.Log_line();
            streamReader.Close();
            httpWebResponse.Close();
            T res = default(T);
            try
            {
                //json
                res = JsonConvert.DeserializeObject<T>(rspContent);
                return res;
            }
            catch (JsonReaderException e1)
            {
                //处理xml
                var xml = rspContent;
                var xmlStart = xml.IndexOf("<Response");
                var xmlEnd = xml.IndexOf("</Response");
                xml = xml.Substring(xmlStart, xmlEnd - xmlStart) + "</Response>";
                //替换xml根节点
                xmlStart = xml.IndexOf(">") + 1;
                xml = "<Response>" + xml.Substring(xmlStart);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);

                //将xml转化为json
                var json = JsonConvert.SerializeXmlNode(doc);

                //处理json
                json = json.Substring(json.IndexOf(":") + 1);
                if (json.Contains("{\"Item\":"))
                {
                    json = json.Replace("{\"Item\":", "");
                    json = json.Substring(0, json.Length - 2);
                }
                else
                {
                    json = json.Substring(0, json.Length - 1);
                }
                //json反序列化
                res = JsonConvert.DeserializeObject<T>(json);
                Global.log.Debug(e1.Message);
                return res;
            }
            catch (Exception e2)
            {
                Global.log.Debug(e2.Message);
                throw e2;
            }
        }

        public static T Post<T>(string uri, string data)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            byte[] bs = Encoding.UTF8.GetBytes(data);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.ContentLength = bs.Length;
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;
            httpWebRequest.GetRequestStream().Write(bs, 0, bs.Length);
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
            string rspContent = streamReader.ReadToEnd();
            Global.log.Info(uri);
            Global.log.Info("[POST] " + rspContent);
            Global.Log_line();
            streamReader.Close();
            httpWebResponse.Close();
            httpWebRequest.Abort();
            T res;
            try
            {
                res = JsonConvert.DeserializeObject<T>(rspContent);
            }
            catch (Exception e)
            {
                Global.log.Debug(e.Message);
                var json = JsonConvert.SerializeObject(rspContent);
                res = JsonConvert.DeserializeObject<T>(json);
            }
            return res;
        }
    }
}